package com.example.pets_java.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.client.WebClient;
import com.example.pets_java.utils.SignatureUtil;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@RestController
@RequestMapping("/api/art")
public class PetArtController {

    @Autowired
    private WebClient webClient;

    @Autowired
    private SignatureUtil signatureUtil;

    @PostMapping("/create")
    public Mono<ResponseEntity<String>> createArt(@RequestBody Map<String, Object> request, @RequestHeader("Authorization") String token) {
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("dataId", UUID.randomUUID().toString());
        requestBody.put("businessCode", "pc");
        requestBody.put("prompt", request.get("prompt"));
        requestBody.put("styleConfig", "55c682d5eeca50d4806fd1cba3628781");
        requestBody.put("height", 512);
        requestBody.put("width", 512);
        requestBody.put("imageFormat", "JPEG");

        Map<String, String> queryParams = new HashMap<>();
        Map<String, String> headers = signatureUtil.generateAuthHeaders("POST", "/api/v1/task_submit", queryParams);

        return webClient.post()
                .uri("https://api-ai.vivo.com.cn/api/v1/task_submit")
                .headers(httpHeaders -> httpHeaders.setAll(headers))
                .bodyValue(requestBody)
                .retrieve()
                .bodyToMono(String.class)
                .map(body -> ResponseEntity.ok().body(body))
                .onErrorResume(e -> {
                    e.printStackTrace();
                    return Mono.just(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error processing your request: " + e.getMessage()));
                });
    }

    @GetMapping("/status/{taskId}")
    public Mono<ResponseEntity<String>> getArtStatus(@PathVariable String taskId, @RequestHeader("Authorization") String token) {
        Map<String, String> queryParams = new HashMap<>();
        queryParams.put("task_id", taskId);

        MultiValueMap<String, String> multiValueQueryParams = new LinkedMultiValueMap<>();
        queryParams.forEach(multiValueQueryParams::add);

        Map<String, String> headers = signatureUtil.generateAuthHeaders("GET", "/api/v1/task_progress", queryParams);

        return webClient.get()
                .uri(uriBuilder -> uriBuilder.path("/api/v1/task_progress").queryParams(multiValueQueryParams).build())
                .headers(httpHeaders -> httpHeaders.setAll(headers))
                .retrieve()
                .bodyToMono(String.class)
                .map(body -> ResponseEntity.ok().body(body))
                .onErrorResume(e -> {
                    e.printStackTrace();
                    return Mono.just(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error processing your request: " + e.getMessage()));
                });
    }
}